home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 269 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  122 lines

  1. Path: gate.net!pslfl2-24
  2. From: bhutto@gate.net (William Hutto)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointers to structures
  5. Date: 3 Jan 1996 22:12:30 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4cev0e$1dhu@news.gate.net>
  8. References: <4cc9r4$m26@armitage.cyberspace.com> <4ccpgv$qpl@ixnews8.ix.netcom.com> <4cer8r$1v44@news.gate.net>
  9. NNTP-Posting-Host: pslfl2-39.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4cer8r$1v44@news.gate.net>, bhutto@gate.net (William Hutto) wrote:
  13. >icarus@loomis (Tel Janin Aellinsar) wrote:
  14. >
  15. >>Berserker Dragon, Knights of the Cosmos             icarus@BERKSHIRE.NET
  16. >
  17. >> I'm having trouble using a pointer to a struct.  The idea is to
  18. >> have a struct get filled by a function, which gets the address
  19. >> and everything via function(struct type*).  Very straightforward.
  20. >> However, if I try to CHANGE anything in the struct, it just goes
  21. >> back when the function is over.  So, let's pretend this imaginary
  22. >> structure is what I'm using:
  23. >>
  24. >> struct st1 {
  25. >>   char *name;
  26. >>   int yadda;
  27. >> };
  28. >>
  29. >> And the function is:
  30. >>
  31. >> fn1(struct st1 *st)
  32. >
  33. >If you want to assigned values to be retained, this needs to be double 
  34. >indirection.
  35. >
  36. >    void fn1(struct st1 **st)
  37. >
  38. >> {
  39. >>   st = (struct st1*)malloc(sizeof(struct st1*));
  40. >                                    ^^^^^^^^^^^
  41. >This allocates enough space for a pointer, not your structure. Nor does it 
  42. >check validity before usage. This should be:
  43. >
  44. >    if((*st=malloc(sizeof(struct st1))==NULL) {
  45. >        /*error*/
  46. >    }        
  47. >
  48. >Also note the indirection: *st
  49. >
  50. >st points to the pointer that you pass to this function. That's where 
  51. >malloc()'s returned value is going.
  52. >
  53. >>   st->name = (char*)malloc(16);
  54. >
  55. >    if((*st->name=malloc(16))==NULL) {
  56. >        /*error*/
  57. >    }
  58. >
  59. >>   strcpy(st->name,"Test");
  60. >
  61. >    strcpy(*st->name,"Test");
  62. >
  63. >>   st->yadda = 100;
  64. >
  65. >    *st->yadda = 100;
  66. >
  67. >> }
  68. >
  69. >You must call this function with the address of a pointer of struct type.
  70. >
  71. >func()
  72. >{
  73. >struct st1 *stptr;
  74. >
  75. >    fn1(&stptr);
  76. >    printf("%s, %d\n",stptr->name,stptr->yadda);
  77. >    
  78. >}                
  79.  
  80. I should have compiled this before posting it. I wanted to post an 
  81. additional way of doing this anyway, which should be easier. The indirect 
  82. member of (->) operator has higher precedence than the indirection (*) 
  83. operator, therefore when accessing the members of a structure in this way you 
  84. need to force the compiler to dereference the pointer to the pointer first:
  85.  
  86.     (*st)->name
  87.  
  88. The easier way to do this is just to return a pointer to a structure:
  89.  
  90. func()
  91. {
  92. struct st1 *stptr;
  93.  
  94.     stptr=fn1();
  95.     printf("%s, %d\n",stptr->name,stptr->yadda);
  96.  
  97. }
  98.  
  99. struct st1 *fn1(void)
  100. {
  101. struct st1 *st;
  102.  
  103.     if((st=malloc(sizeof(struct st1)))==NULL) {
  104.         /*error*/
  105.     }        
  106.     if((st->name=malloc(16))==NULL) {
  107.         /*error*/
  108.     }
  109.     strcpy(st->name,"Test");
  110.     st->yadda = 100;
  111.     return st;
  112. }
  113.  
  114. This accomplishes the same result, but is less convoluted.
  115.  
  116. Bill
  117.  
  118.  
  119.  
  120.  
  121. "Whatcha got on?...Your mind?"
  122.